package org.liveboardgames.agricola.business.action.processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.liveboardgames.agricola.domain.Change;
import org.liveboardgames.agricola.domain.Game;
import org.liveboardgames.agricola.domain.action.Action;
import org.liveboardgames.agricola.domain.player.Player;
import org.liveboardgames.agricola.model.enums.ChangesEnum;

public class FirstPlayerActionProcessor extends ActionProcessor {

  public FirstPlayerActionProcessor(Game game, Action action, List<Change> changes) {
    super(game, action, changes);
  }

  @Override
  public void execute() {
    Player newFirstPlayer = (Player)action.getTarget();
    List<Player> players = game.getPlayers();
    int playerIndex = players.indexOf(newFirstPlayer);
    Collections.rotate(players, -playerIndex);

    List<Long> playerOrder = new ArrayList<Long>();
    for (Player player : players) {
      playerOrder.add(player.getId());
    }
    Change change = new Change(ChangesEnum.PLAYER_ORDER, playerOrder);
    changes.add(change);
  }

}
